﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BidFunLib.Services;
using log4net;
using BidFunLib.Entity;
using BidFunLib.BidWeb;

namespace BidFunLib.EntityHelper
{
    //
    class BidSaleEndStatisticsCalculator
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(BidSaleEndStatisticsCalculator));

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static BidSaleEndStatisticsCalculator instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static BidSaleEndStatisticsCalculator Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BidSaleEndStatisticsCalculator();
                }
                return BidSaleEndStatisticsCalculator.instance;
            }
            set
            {
                BidSaleEndStatisticsCalculator.instance = value;
            }
        }

        private BidSaleEndStatisticsCalculator()
        {
        }

        /// <summary>
        /// Cette methode calcule automatiquement toutes les statistiques relatives à une vente.
        /// 
        /// </summary>
        /// <param name="idBidSale"></param>
        /// <param name="?"></param>
        /// <param name="usersWithBid"></param>
        public StatsBidSale caculateStatisticsAtTheEndOfBidSale(BidSale bidSale, Dictionary<String, BidUserWeb> usersWithBid) 
        {
            if (bidSale != null && usersWithBid != null)
            {
                Dictionary<Int32, StatsBidUserByBidSale> bidSaleMapForBidUsers = null;
                // 1- calculate bidSale end statistics
                StatsBidSale statsBidSale = StatsBidSaleManager.Instance.calculateStatsBidSale(bidSale, usersWithBid);

                // 2- calacule bidObject statistics 
                StatsBidObjectGlobalManager.Instance.calculateStatsBidObjectGlobal(statsBidSale);



                // 3- calculates biduser by bid sale statistics
                bidSaleMapForBidUsers = StatsBidUserByBidSaleManager.Instance.calculateStatsBidUserByBidSale(bidSale, usersWithBid);

                if (bidSaleMapForBidUsers != null)
                {
                    // 4- calculates biduser by bidObject statistics
                    StatsBidUserByBidObjectManager.Instance.calculateStatsBidUserByBidObject(bidSale, bidSaleMapForBidUsers);

                    // 5- calculates biduser global statistics
                    StatsBidUserGlobalManager.Instance.calculateStatsBidUserGlobal(bidSale, bidSaleMapForBidUsers);
                }

                return statsBidSale;
            }

            return null;
        }

    }
}
